package com.chat.config;

import com.chat.handler.ChatWebSocketHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * WebSocket配置类
 * 配置WebSocket端点和处理器
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Autowired
    private ChatWebSocketHandler chatWebSocketHandler;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        System.out.println("注册WebSocket处理器到端点: /chat");
        // 注册WebSocket处理器，设置端点为/chat
        registry.addHandler(chatWebSocketHandler, "/chat")
                .setAllowedOriginPatterns("*") // 允许所有来源模式
                .withSockJS(); // 启用SockJS支持以提高兼容性
        System.out.println("WebSocket处理器注册完成");
    }
}